﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows;
using System.ComponentModel;

namespace Bixas.Tangibilis.Controls.Primitives
{
    public class TouchThumb : Thumb, ITouchCursorEvent, ITouchCursorEventThunk, ITouchElement
    {
        #region ITouchCursorEvent Members

        public event System.Windows.RoutedEventHandler TouchCursorDown
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorDownEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorDownEvent, value);
            }
        }

        public event System.Windows.RoutedEventHandler TouchCursorEnter
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorEnterEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorEnterEvent, value);
            }
        }

        public event System.Windows.RoutedEventHandler TouchCursorLeave
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorLeaveEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorLeaveEvent, value);
            }
        }

        public event System.Windows.RoutedEventHandler TouchCursorMove
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorMoveEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorMoveEvent, value);
            }
        }

        public event System.Windows.RoutedEventHandler TouchCursorUp
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorUpEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorUpEvent, value);
            }
        }

        public event System.Windows.RoutedEventHandler TouchCursorPreviewDown
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorPreviewDownEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorPreviewDownEvent, value);
            }
        }

        public event System.Windows.RoutedEventHandler TouchCursorPreviewMove
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorPreviewMoveEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorPreviewMoveEvent, value);
            }
        }

        public event System.Windows.RoutedEventHandler TouchCursorPreviewUp
        {
            add
            {
                this.AddHandler(TouchCursorEvents.TouchCursorPreviewUpEvent, value, false);
            }
            remove
            {
                this.RemoveHandler(TouchCursorEvents.TouchCursorPreviewUpEvent, value);
            }
        }
        #endregion

        private TouchThumbAdapter _adapter;

        private static readonly DependencyPropertyKey IsTouchCursorOverPropertyKey;
        public static readonly DependencyProperty IsTouchCursorOverProperty;

        private static readonly DependencyPropertyKey IsTouchCursorPressedPropertyKey;
        public static readonly DependencyProperty IsTouchCursorPressedProperty;
        
        private static readonly DependencyPropertyKey IsTouchDraggingPropertyKey;
        public static readonly DependencyProperty IsTouchDraggingProperty;
        private static readonly DependencyPropertyKey IsTouchRotatingPropertyKey;
        public static readonly DependencyProperty IsTouchRotatingProperty;
        private static readonly DependencyPropertyKey IsTouchScalingPropertyKey;
        public static readonly DependencyProperty IsTouchScalingProperty;

        public static readonly RoutedEvent TouchDragCompletedEvent;
        public static readonly RoutedEvent TouchDragDeltaEvent;
        public static readonly RoutedEvent TouchDragStartedEvent;

        public static readonly RoutedEvent TouchRotateCompletedEvent;
        public static readonly RoutedEvent TouchRotateDeltaEvent;
        public static readonly RoutedEvent TouchRotateStartedEvent;

        public static readonly RoutedEvent TouchScaleCompletedEvent;
        public static readonly RoutedEvent TouchScaleDeltaEvent;
        public static readonly RoutedEvent TouchScaleStartedEvent;

        public TouchThumb()
            : base()
        {
            this._adapter = new TouchThumbAdapter(this);
        }

        static TouchThumb()
        {
            //FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchButton), new FrameworkPropertyMetadata(typeof(TouchButton)));
            TouchDragStartedEvent = EventManager.RegisterRoutedEvent("TouchDragStarted", RoutingStrategy.Bubble, typeof(TouchDragStartedEventHandler), typeof(TouchThumb));
            TouchDragDeltaEvent = EventManager.RegisterRoutedEvent("TouchDragDelta", RoutingStrategy.Bubble, typeof(TouchDragDeltaEventHandler), typeof(TouchThumb));
            TouchDragCompletedEvent = EventManager.RegisterRoutedEvent("TouchDragCompleted", RoutingStrategy.Bubble, typeof(TouchDragCompletedEventHandler), typeof(TouchThumb));

            TouchRotateStartedEvent = EventManager.RegisterRoutedEvent("TouchRotateStarted", RoutingStrategy.Bubble, typeof(TouchRotateStartedEventHandler), typeof(TouchThumb));
            TouchRotateDeltaEvent = EventManager.RegisterRoutedEvent("TouchRotateDelta", RoutingStrategy.Bubble, typeof(TouchRotateDeltaEventHandler), typeof(TouchThumb));
            TouchRotateCompletedEvent = EventManager.RegisterRoutedEvent("TouchRotateCompleted", RoutingStrategy.Bubble, typeof(TouchRotateCompletedEventHandler), typeof(TouchThumb));

            TouchScaleStartedEvent = EventManager.RegisterRoutedEvent("TouchScaleStarted", RoutingStrategy.Bubble, typeof(TouchScaleStartedEventHandler), typeof(TouchThumb));
            TouchScaleDeltaEvent = EventManager.RegisterRoutedEvent("TouchScaleDelta", RoutingStrategy.Bubble, typeof(TouchScaleDeltaEventHandler), typeof(TouchThumb));
            TouchScaleCompletedEvent = EventManager.RegisterRoutedEvent("TouchScaleCompleted", RoutingStrategy.Bubble, typeof(TouchScaleCompletedEventHandler), typeof(TouchThumb));
        
            IsTouchDraggingPropertyKey = DependencyProperty.RegisterReadOnly("IsTouchDragging", typeof(bool), typeof(TouchThumb), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(TouchThumb.OnIsTouchDraggingPropertyChanged)));
            IsTouchDraggingProperty = IsTouchDraggingPropertyKey.DependencyProperty;
            IsTouchRotatingPropertyKey = DependencyProperty.RegisterReadOnly("IsTouchRotating", typeof(bool), typeof(TouchThumb), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(TouchThumb.OnIsTouchRotatingPropertyChanged)));
            IsTouchRotatingProperty = IsTouchRotatingPropertyKey.DependencyProperty;
            IsTouchScalingPropertyKey = DependencyProperty.RegisterReadOnly("IsTouchScaling", typeof(bool), typeof(TouchThumb), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(TouchThumb.OnIsTouchScalingPropertyChanged)));
            IsTouchScalingProperty = IsTouchScalingPropertyKey.DependencyProperty;

            IsTouchCursorOverPropertyKey = DependencyProperty.RegisterReadOnly("IsTouchCursorOver", typeof(bool), typeof(TouchThumb), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(TouchThumb.OnIsTouchCursorOverPropertyChanged)));
            IsTouchCursorOverProperty = IsTouchCursorOverPropertyKey.DependencyProperty;

            IsTouchCursorPressedPropertyKey = DependencyProperty.RegisterReadOnly("IsTouchCursorPressed", typeof(bool), typeof(TouchThumb), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(TouchThumb.OnIsTouchCursorPressedPropertyChanged)));
            IsTouchCursorPressedProperty = IsTouchCursorPressedPropertyKey.DependencyProperty;

        }

        #region ITouchCursorEventThunk Members

        public void OnTouchCursorDownThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorDown(e);
        }

        public void OnTouchCursorEnterThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorEnter(e);
        }

        public void OnTouchCursorLeaveThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorLeave(e);
        }

        public void OnTouchCursorMoveThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorMove(e);
        }

        public void OnTouchCursorUpThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorUp(e);
        }

        public void OnTouchCursorPreviewDownThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorPreviewDown(sender, e);
        }

        public void OnTouchCursorPreviewMoveThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorPreviewMove(sender, e);
        }

        public void OnTouchCursorPreviewUpThunk(object sender, System.Windows.RoutedEventArgs e)
        {
            this._adapter.OnTouchCursorPreviewUp(sender, e);
        }

        #endregion

        [Category("Behavior")]
        public event TouchDragCompletedEventHandler TouchDragCompleted
        {
            add
            {
                base.AddHandler(TouchDragCompletedEvent, value);
            }
            remove
            {
                base.RemoveHandler(TouchDragCompletedEvent, value);
            }
        }

        [Category("Behavior")]
        public event TouchDragDeltaEventHandler TouchDragDelta
        {
            add
            {
                base.AddHandler(TouchDragDeltaEvent, value);
            }
            remove
            {
                base.RemoveHandler(TouchDragDeltaEvent, value);
            }
        }

        [Category("Behavior")]
        public event TouchDragStartedEventHandler TouchDragStarted
        {
            add
            {
                base.AddHandler(TouchDragStartedEvent, value);
            }
            remove
            {
                base.RemoveHandler(TouchDragStartedEvent, value);
            }
        }

        [Category("Behavior")]
        public event TouchRotateCompletedEventHandler TouchRotateCompleted
        {
            add
            {
                base.AddHandler(TouchRotateCompletedEvent, value);
            }
            remove
            {
                base.RemoveHandler(TouchRotateCompletedEvent, value);
            }
        }

        [Category("Behavior")]
        public event TouchRotateDeltaEventHandler TouchRotateDelta
        {
            add
            {
                base.AddHandler(TouchRotateDeltaEvent, value);
            }
            remove
            {
                base.RemoveHandler(TouchRotateDeltaEvent, value);
            }
        }

        [Category("Behavior")]
        public event TouchRotateStartedEventHandler TouchRotateStarted
        {
            add
            {
                base.AddHandler(TouchRotateStartedEvent, value);
            }
            remove
            {
                base.RemoveHandler(TouchRotateStartedEvent, value);
            }
        }

        [Category("Behavior")]
        public event TouchScaleCompletedEventHandler TouchScaleCompleted
        {
            add
            {
                base.AddHandler(TouchScaleCompletedEvent, value);
            }
            remove
            {
                base.RemoveHandler(TouchScaleCompletedEvent, value);
            }
        }

        [Category("Behavior")]
        public event TouchScaleDeltaEventHandler TouchScaleDelta
        {
            add
            {
                base.AddHandler(TouchScaleDeltaEvent, value);
            }
            remove
            {
                base.RemoveHandler(TouchScaleDeltaEvent, value);
            }
        }

        [Category("Behavior")]
        public event TouchScaleStartedEventHandler TouchScaleStarted
        {
            add
            {
                base.AddHandler(TouchScaleStartedEvent, value);
            }
            remove
            {
                base.RemoveHandler(TouchScaleStartedEvent, value);
            }
        }


        private static void OnIsTouchDraggingPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TouchThumb)d).OnTouchDraggingChanged(e);
        }

        protected virtual void OnTouchDraggingChanged(DependencyPropertyChangedEventArgs e)
        {
        }
        
        private static void OnIsTouchRotatingPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TouchThumb)d).OnTouchRotatingChanged(e);
        }

        protected virtual void OnTouchRotatingChanged(DependencyPropertyChangedEventArgs e)
        {
        }

        private static void OnIsTouchScalingPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TouchThumb)d).OnTouchScalingChanged(e);
        }

        protected virtual void OnTouchScalingChanged(DependencyPropertyChangedEventArgs e)
        {
        }


        private static void OnIsTouchCursorOverPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TouchThumb)d).OnTouchCursorOverChanged(e);
        }

        protected virtual void OnTouchCursorOverChanged(DependencyPropertyChangedEventArgs e)
        {
        }

        private static void OnIsTouchCursorPressedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TouchThumb)d).OnTouchCursorPressedChanged(e);
        }

        protected virtual void OnTouchCursorPressedChanged(DependencyPropertyChangedEventArgs e)
        {
        }
        
        #region ITouchElement Members

        bool ITouchElement.IsTouchCursorOver
        {
            get
            {
                return (bool)base.GetValue(IsTouchCursorOverProperty);
            }
            set
            {
                base.SetValue(IsTouchCursorOverPropertyKey, value);
                // this.CrackedWriteFlag(0x2000, value);
            }
        }

        bool ITouchElement.IsTouchCursorPressed
        {
            get
            {
                return (bool)base.GetValue(IsTouchCursorPressedProperty);
            }
            set
            {
                base.SetValue(IsTouchCursorPressedPropertyKey, value);
            }
        }

        
        #endregion

        public bool IsTouchDragging
        {
            get
            {
                return (bool)base.GetValue(IsTouchDraggingProperty);
            }
            set
            {
                base.SetValue(IsTouchDraggingPropertyKey, value);
            }
        }

        public bool IsTouchRotating
        {
            get
            {
                return (bool)base.GetValue(IsTouchDraggingProperty);
            }
            set
            {
                base.SetValue(IsTouchDraggingPropertyKey, value);
            }
        }

        public bool IsTouchScaling
        {
            get
            {
                return (bool)base.GetValue(IsTouchDraggingProperty);
            }
            set
            {
                base.SetValue(IsTouchDraggingPropertyKey, value);
            }
        }
    }
}
